package com.yanqu.road.server.cmd.voucher;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.voucher.UserVoucherData;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.voucher.VoucherProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.gameplayer.module.player.VoucherModule;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_VOUCHER_USE_FROM_CROSS, desc = "使用代金券跨域返回")
public class VoucherUseFromCrossCmd extends AbstractCommand {

    private String preCost = "";

    private String realCost = "";

    private List<VoucherProto.UserVoucherDataTemp> voucherInfoTempList = new ArrayList<>();

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {

        VoucherProto.ChannelVoucherUseRespMsg reqMsg = VoucherProto.ChannelVoucherUseRespMsg.parseFrom(packet.getBytes());

        if (reqMsg.hasPreCost()) {
            preCost = reqMsg.getPreCost();
        }
        if (reqMsg.hasRealCost()) {
            realCost = reqMsg.getRealCost();
        }
        if (reqMsg.getVoucherInfoTempList() != null) {
            voucherInfoTempList = reqMsg.getVoucherInfoTempList();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        // 取消预扣除
        player.getModule(PreReduceModule.class).restorePreReduce(preCost);

        if (ret == 0) {
            // 扣除代金券
            player.getModule(CurrencyModule.class).removeCurrency(PropertyHelper.parseStringToProperty(realCost), eLogMoneyType.System, eLogMoneyType.VoucherCost);

            List<UserVoucherData> userVoucherDataList = new ArrayList<>();
            // 给玩家添加代金券
            for (VoucherProto.UserVoucherDataTemp voucherInfoTemp : voucherInfoTempList) {
                UserVoucherData userVoucherData = new UserVoucherData();
                userVoucherData.setUserId(player.getUserId());
                userVoucherData.setVoucherId(voucherInfoTemp.getVoucherId());
                userVoucherData.setCode(voucherInfoTemp.getCode());
                userVoucherData.setType(voucherInfoTemp.getType());
                userVoucherData.setPrice(voucherInfoTemp.getPrice());
                userVoucherData.setExpireTime(voucherInfoTemp.getExpireTime());
                userVoucherData.setInsertOption();
                userVoucherDataList.add(userVoucherData);
            }
            if (userVoucherDataList.size() > 0) {
                player.getModule(VoucherModule.class).addVoucherData(userVoucherDataList);
            }
        }
    }


    @Override
    public void sendData(GamePlayer player) {

    }
}
